﻿using DevExpress.XtraEditors;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Shopfloormanagement
{
    public partial class Step5fish : DevExpress.XtraEditors.XtraForm
    {
        public Step5fish()
        {
            InitializeComponent();
        }
        int step1;
        int step2;
        int step3;
        int step4;
        int step5;
        int step6;
        int step7;
        int step8;
        int step9;
        private void Word()//加载进度下方文字
        {
            lblStep1.Text = "Problem\nStatement";
            lblStep2.Text = "Problem\nDescription";
            lblStep3.Text = "Interim\nContainment\nactions";
            lblStep4.Text = "Identify\nPossible\nCauses";
            lblStep5.Text = "Test all\nPossible\nCauses";
            lblStep6.Text = "Verify\nPotential\nRoot Cause";
            lblStep7.Text = "Confirmed\nRoot Cause";
            lblStep8.Text = "Permanent\nCorrective\nActions";
            lblStep9.Text = "Further\nActivities";

        }
        private void LoadA3list()//获取上方图片状态
        {

            DataTable A3list = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");

            step1 = (int)A3list.Rows[0][6];
            step2 = (int)A3list.Rows[0][7];
            step3 = (int)A3list.Rows[0][8];
            step4 = (int)A3list.Rows[0][9];
            step5 = (int)A3list.Rows[0][10];
            step6 = (int)A3list.Rows[0][11];
            step7 = (int)A3list.Rows[0][12];
            step8 = (int)A3list.Rows[0][13];
            step9 = (int)A3list.Rows[0][14];
            LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
        }
        private void LoadPicture(int condition1, int condition2, int condition3, int condition4, int condition5, int condition6, int condition7, int condition8, int condition9)
        {
            switch (condition1)
            {
                case 1:
                    picstep1.Image = Properties.Resources.G1;
                    break;
                case 2:
                    picstep1.Image = Properties.Resources.B1;
                    break;
                case 3:
                    picstep1.Image = Properties.Resources.R1;
                    break;
                case 4:
                    picstep1.Image = Properties.Resources.H1;
                    break;
                default:
                    picstep1.Image = null;
                    break;
            }
            switch (condition2)
            {
                case 1:
                    picstep2.Image = Properties.Resources.G2;
                    break;
                case 2:
                    picstep2.Image = Properties.Resources.B2;
                    break;
                case 3:
                    picstep2.Image = Properties.Resources.R2;
                    break;
                case 4:
                    picstep2.Image = Properties.Resources.H2;
                    break;
                default:
                    picstep2.Image = null;
                    break;
            }
            switch (condition3)
            {
                case 1:
                    picstep3.Image = Properties.Resources.G3;
                    break;
                case 2:
                    picstep3.Image = Properties.Resources.B3;
                    break;
                case 3:
                    picstep3.Image = Properties.Resources.R3;
                    break;
                case 4:
                    picstep3.Image = Properties.Resources.H3;
                    break;
                default:
                    picstep3.Image = null;
                    break;
            }
            switch (condition4)
            {
                case 1:
                    picstep4.Image = Properties.Resources.G4;
                    break;
                case 2:
                    picstep4.Image = Properties.Resources.B4;
                    break;
                case 3:
                    picstep4.Image = Properties.Resources.R4;
                    break;
                case 4:
                    picstep4.Image = Properties.Resources.H4;
                    break;
                default:
                    picstep4.Image = null;
                    break;
            }
            switch (condition5)
            {
                case 1:
                    picstep5.Image = Properties.Resources.G5;
                    break;
                case 2:
                    picstep5.Image = Properties.Resources.B5;
                    break;
                case 3:
                    picstep5.Image = Properties.Resources.R5;
                    break;
                case 4:
                    picstep5.Image = Properties.Resources.H5;
                    break;
                default:
                    picstep5.Image = null;
                    break;
            }
            switch (condition6)
            {
                case 1:
                    picstep6.Image = Properties.Resources.G6;
                    break;
                case 2:
                    picstep6.Image = Properties.Resources.B6;
                    break;
                case 3:
                    picstep6.Image = Properties.Resources.R6;
                    break;
                case 4:
                    picstep6.Image = Properties.Resources.H6;
                    break;
                default:
                    picstep6.Image = null;
                    break;
            }
            switch (condition7)
            {
                case 1:
                    picstep7.Image = Properties.Resources.G7;
                    break;
                case 2:
                    picstep7.Image = Properties.Resources.B7;
                    break;
                case 3:
                    picstep7.Image = Properties.Resources.R7;
                    break;
                case 4:
                    picstep7.Image = Properties.Resources.H7;
                    break;
                default:
                    picstep7.Image = null;
                    break;
            }
            switch (condition8)
            {
                case 1:
                    picstep8.Image = Properties.Resources.G8;
                    break;
                case 2:
                    picstep8.Image = Properties.Resources.B8;
                    break;
                case 3:
                    picstep8.Image = Properties.Resources.R8;
                    break;
                case 4:
                    picstep8.Image = Properties.Resources.H8;
                    break;
                default:
                    picstep8.Image = null;
                    break;
            }
            switch (condition9)
            {
                case 1:
                    picstep9.Image = Properties.Resources.G9;
                    break;
                case 2:
                    picstep9.Image = Properties.Resources.B9;
                    break;
                case 3:
                    picstep9.Image = Properties.Resources.R9;
                    break;
                case 4:
                    picstep9.Image = Properties.Resources.H9;
                    break;
                default:
                    picstep9.Image = null;
                    break;
            }

        }
        private void LoadA3step5fish()//加载信息及判断是否过期
        {
            DataTable Fish = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step4", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (Fish.Rows.Count == 0) return; // 如果没有数据，直接返回
            else { Fishbone.Text = Fish.Rows[0][2].ToString(); }
            DataRow row = Fish.Rows[0]; // 取第 0 行数据
            // 加载Man
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Man{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m1{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Machine
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Machine{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m2{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Material
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Material{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m3{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Method
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Method{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m4{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Mileu
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Mileu{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m5{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            //加载Measurement
            for (int i = 1; i <= 5; i++)
            {
                string columnName = $"Measurement{i}";

                // 确保列存在
                if (!Fish.Columns.Contains(columnName)) continue;

                // 获取 lablecontrol 控件
                Control[] controls = this.Controls.Find($"m6{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.LabelControl lablecontrol)
                {
                    // 赋值，忽略 NULL
                    lablecontrol.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
                      
            
        }
        private void LoadDataAndUpdateControls()
        {
            // 从数据库中获取数据表
            DataTable A3Step5 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step5", $"A3ReportNumberID = '{rootcause.A3number}'");

            if (A3Step5.Rows.Count > 0)
            {
                // 遍历表格中的每一行
                foreach (DataRow row in A3Step5.Rows)
                {
                    // 遍历 m1{i}, m2{i} ... m6{i} (i从1到5)
                    for (int group = 1; group <= 6; group++)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            // 获取 LabelControl 和 PictureEdit 控件
                            LabelControl labelControl = (LabelControl)this.Controls.Find($"m{group}{i}", true).FirstOrDefault();
                            PictureEdit pictureEdit = (PictureEdit)this.Controls.Find($"p{group}{i}", true).FirstOrDefault();

                            if (labelControl != null && pictureEdit != null)
                            {
                                bool matchFound = false; // 记录是否匹配成功

                                // 遍历 Cause1 到 Cause30
                                for (int j = 1; j <= 30; j++)
                                {
                                    string causeColumn = $"Cause{j}";
                                    string directColumn = $"Direct{j}";
                                    string notColumn = $"Not{j}";
                                    string doubtfulColumn = $"Doubtful{j}";

                                    if (labelControl.Text == row[causeColumn].ToString()) // 如果匹配成功
                                    {
                                        int directValue = Convert.ToInt32(row[directColumn]);
                                        int notValue = Convert.ToInt32(row[notColumn]);
                                        int doubtfulValue = Convert.ToInt32(row[doubtfulColumn]);

                                        // 根据条件设置图片
                                        SetPictureBasedOnCondition(pictureEdit, directValue, notValue, doubtfulValue);

                                        matchFound = true;
                                        break; // 找到匹配后就跳出循环
                                    }
                                }

                                // 如果没有匹配到任何 Cause，隐藏 PictureEdit
                                if (!matchFound)
                                {
                                    pictureEdit.Visible = false;
                                }
                            }
                        }
                    }
                }
            }
        }

        // 根据条件判断并设置图片
        private void SetPictureBasedOnCondition(PictureEdit pictureEdit, int directValue, int notValue, int doubtfulValue)
        {
            pictureEdit.EditValue = null;

            if (directValue == 1)
            {
                pictureEdit.EditValue = Properties.Resources.对号; // B1 图片
            }
            else if (notValue == 1)
            {
                pictureEdit.EditValue = Properties.Resources.错号; // B2 图片
            }
            else if (doubtfulValue == 1)
            {
                pictureEdit.EditValue = Properties.Resources.问号; // B3 图片
            }

            // 如果没有图片，则隐藏控件
            pictureEdit.Visible = pictureEdit.EditValue != null;

            // 设置图片填充模式
            pictureEdit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
        }



        private void Step5fish_Load(object sender, EventArgs e)
        {
            Word();
            LoadA3list();
            LoadA3step5fish();
            LoadDataAndUpdateControls();
            palstep1.BackColor = Color.FromArgb(146, 208, 80);
            palstep2.BackColor = Color.FromArgb(146, 208, 80);
            palstep3.BackColor = Color.FromArgb(146, 208, 80);
            palstep4.BackColor = Color.FromArgb(146, 208, 80);
            palstep5.BackColor = Color.FromArgb(146, 208, 80);

        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            Step6 frm = new Step6();
            frm.Show();
            this.Hide();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            
            Step5 frm = new Step5();
            frm.Show();
            this.Hide();

        }

        private void btnClosed_Click(object sender, EventArgs e)
        {
            // 弹出确认对话框，询问是否关闭
            DialogResult result = MessageBox.Show("是否确定关闭？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                // 用户选择“是”，执行关闭操作
                this.Close(); // 关闭当前窗体
            }
            else
            {
                // 用户选择“否”，返回，不做任何操作
                return;
            }
        }
        //点击图片跳转功能
        private void picstep1_Click(object sender, EventArgs e)
        {
            if (step1 == 4)
            {
                return;
            }
            else
            {
                Step1 frm = new Step1();
                this.Close();
                frm.Show();
            }
        }

        private void picstep2_Click(object sender, EventArgs e)
        {
            if (step2 == 4)
            {
                return;
            }
            else
            {
                Step2 frm = new Step2();
                this.Close();
                frm.Show();
            }
        }

        private void picstep3_Click(object sender, EventArgs e)
        {
            if (step3 == 4)
            {
                return;
            }
            else
            {
                Step3 frm = new Step3();
                this.Close();
                frm.Show();
            }
        }
        private void picstep4_Click(object sender, EventArgs e)
        {
            if (step4 == 4)
            {
                return;
            }
            else
            {
                Step4 frm = new Step4();
                this.Close();
                frm.Show();
            }
        }

        private void picstep5_Click(object sender, EventArgs e)
        {
            if (step5 == 4)
            {
                return;
            }
            else
            {
                Step5 frm = new Step5();
                this.Close();
                frm.Show();
            }
        }

        private void picstep6_Click(object sender, EventArgs e)
        {
            if (step6 == 4)
            {
                return;
            }
            else
            {
                Step6 frm = new Step6();
                this.Close();
                frm.Show();
            }
        }

        private void picstep7_Click(object sender, EventArgs e)
        {
            if (step7 == 4)
            {
                return;
            }
            else
            {
                Step7 frm = new Step7();
                this.Close();
                frm.Show();
            }
        }

        private void picstep8_Click(object sender, EventArgs e)
        {
            if (step8 == 4)
            {
                return;
            }
            else
            {
                Step8 frm = new Step8();
                this.Close();
                frm.Show();
            }
        }

        private void picstep9_Click(object sender, EventArgs e)
        {
            if (step9 == 4)
            {
                return;
            }
            else
            {
                Step9 frm = new Step9();
                this.Close();
                frm.Show();
            }
        }
    }
}